package com.athuahua.spzx.controller.cargo;

import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.vo.ProductVo;
import com.athuahua.spzx.dto.product.ProductDto;
import com.athuahua.spzx.entity.ProductSaveUpdateEntity;
import com.athuahua.spzx.service.ProductService;
import com.athuahua.spzx.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/8/1 0001
 * @Time: 15:07
 * @Description:
 */
@RestController
@RequestMapping("admin/product/product")
@Tag(name = "商品管理接口")
@Slf4j
public class ProductController {
    @Resource
    private ProductService productService;

    @GetMapping("pageInfo/{page}/{limit}")
    @Cacheable(cacheNames = "productPageInfo",
            key = "{#p0, #p1}",
            condition = "#p2.brandId == null && #p2.category1Id == null && #p2.category2Id == null && #p2.category3Id == null")
    public Result pageInfo(@PathVariable("page") Integer page,
                           @PathVariable("limit") Integer limit,
                           ProductDto productDto) {
        log.info("page:{}, limit:{}, productDto:{}", page, limit, productDto);
        IPage<ProductVo> productIPage = productService.pageInfo(page, limit, productDto);
        Map<String, Object> map = new HashMap<>();
        map.put("list", productIPage.getRecords());
        map.put("total", productIPage.getTotal());

        return Result.success(map);
    }


    /**
     * 添加要操作的三张表
     * product表 商品基本信息
     * product_sku表 商品的sku信息表
     * product_detail表 商品的详情信息表
     */
    @PostMapping("save")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "添加商品", businessType = 1) //记录日志使用
    public Result save(@RequestBody ProductSaveUpdateEntity product) {
        log.info("save product:{}", product);
        productService.iSave(product);


        return Result.success();
    }

    //修改 updateById
    @PutMapping("updateById")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "修改商品", businessType = 2) //记录日志使用
    public Result updateById(@RequestBody ProductSaveUpdateEntity product) {
        log.info("update product:{}", product);
        productService.iUpdate(product);


        return Result.success();
    }


    //删除 deleteById
    @DeleteMapping("deleteById/{id}")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "删除商品", businessType = 3) //记录日志使用
    public Result deleteById(@PathVariable("id") Integer id) {
        log.info("delete product id:{}", id);
        productService.iRemoveById(id);

        return Result.success();
    }


    //根据商品id查询商品信息
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("get product id:{}", id);
        ProductSaveUpdateEntity productVo = productService.iGetById(id);


        return Result.success(productVo);
    }


    //审核
    @GetMapping("updateAuditStatus/{id}/{status}")
    @CacheEvict(cacheNames = "productPageInfo", allEntries = true)
    @AutoLog(title = "审核商品", businessType = 2) //记录日志使用
    public Result updateAuditStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        log.info("updateAuditStatus id:{}, status:{}", id, status);
        productService.iUpdateAuditStatus(id, status);



        return Result.success();
    }

    //上下架
    @GetMapping("updateStatus/{id}/{status}")
    @Caching(evict = {
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "c_category_product_index", allEntries = true),
    })
    @AutoLog(title = "上下架商品", businessType = 2) //记录日志使用
    public Result updateStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        log.info("updateStatus id:{}, status:{}", id, status);
        productService.iUpdateStatus(id, status);



        return Result.success();
    }

}
